package org.example.day20240905.view;

import org.example.day20240905.dao.ArticleDao;
import org.example.day20240905.model.Article;
import org.example.day20240905.model.Tag;
import org.example.day20240905.model.User;
import org.example.day20240905.util.GlobalInput;

import java.text.SimpleDateFormat;
import java.util.*;

public class ArticleView {
    public void createArticle(){
        Article article = new Article();
        List<Tag> tagList = new ArrayList<>();
        Scanner scanner = GlobalInput.getScanner();
        System.out.println("请输入标题");
        article.setTitle(scanner.nextLine());
        System.out.println("请输入内容");
        article.setBody(scanner.nextLine());
        System.out.println("请输入文章标签");
        String strTag = scanner.nextLine();
        String[] arrTags = strTag.split(",");
        for (String arrTag : arrTags){
            UUID uuid = UUID.randomUUID();
            Tag tag = new Tag();
            tag.setId(uuid.toString());
            tag.setName(arrTag);
            tagList.add(tag);
            article.setTags(tagList);
        }
        UUID uuid = UUID.randomUUID();
        article.setId(uuid.toString());
        article.setUser_id(User.loadUser.getId());
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strDate = sdf.format(date);
        article.setCreated_at(strDate);
        article.setUpdated_at(strDate);
        ArticleDao articleDao = new ArticleDao();
        articleDao.createArticle(article);
    }

    public Map<String,List<Article>> checkArticle(){
        ArticleDao articleDao = new ArticleDao();
        Map<String,List<Article>> articleList = articleDao.checkArticle();
        int count = 0;
        System.out.println("---------所有文章---------");
        for (Map.Entry<String,List<Article>> entry : articleList.entrySet()){
            for (Article article : entry.getValue()){

            }
            System.out.print(entry.getValue().get(count).getNumber() + "." +entry.getValue().get(count).getTitle()+" ");
            System.out.println(entry.getKey()+" ");
            List<Tag> tagList = entry.getValue().get(count).getTags();
            if(!tagList.get(count).getName().equals("")){
                for (Tag tag : tagList){
                    System.out.print(tag.getName()+" ");
                }
                System.out.println();
            }
            count ++;
            System.out.println("-----------------------");
        }
        return articleList;
    }

    public static void viewArticle(int article_id
            , Map<String,List<Article>> mapLst) {
        Scanner scanner = GlobalInput.getScanner();
        boolean isRun = true;
        while (isRun){
            Article map = null;
            String username = "";
            for (Map.Entry<String,List<Article>> entry : mapLst.entrySet()){
                for (int i=1; i<=article_id; i++){
                    map = i==article_id ? entry.getValue().get(article_id) : null;
                    username = i==article_id ? entry.getKey() : "";
                }
            }
            assert map != null;
            System.out.println(map.getTitle());
            System.out.println(map.getBody());
            System.out.println(map.getCreated_at());
            System.out.println(username);
            System.out.println(map.getTags());
            System.out.println("0. 返回到上一层");
            System.out.println("1. 留言");
            System.out.println("2. 点赞");

            int command = scanner.nextInt();
            scanner.nextLine();
            switch (command){
                case 0:{
                    isRun = false;
                    break;
                }
                case 1:{
                    break;
                }
                case 2:{
                    break;
                }
                default:{
                    break;
                }
            }
        }
    }
}
